/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

define(['adminapp', 'core/datatable', 'bootstrap-datepicker', 'bootstrap-datepicker-zh'], function ($app) {
    $app.navactive('member_ticket');
    $('.date-picker').datepicker({
        language      : 'zh-CN',
        rtl: App.isRTL(),
        autoclose: true
    });

    var added_tpl = Handlebars.compile($("#added_template").html());
    var action_tpl = Handlebars.compile($("#action_template").html());
    var pricute_tpl = Handlebars.compile($("#pricute_template").html());
    var color_tpl = Handlebars.compile($("#color_temple").html());
    var errortip_tpl = Handlebars.compile($("#errortip_temple").html());
    var alertcontent = $("#alertcontent_template").html();

    var $listtable = $app.listtable({
        'sAjaxSource' : g.ctx + "admin/ticket/list",
        'fnServerParams': function( aoData){
            var sd = $('#search_form').serializeJSON();
            $.each(sd, function(name, val){
                aoData.push({"name": name, "value": val});
            });
        },
        'aoColumns'   : [
            $app.checkall,
            $app.autono,
            {'mDataProp': 'card_no' },
            {'mDataProp': 'phone' },
            /* {'mDataProp': 'wx_no' },*/
            {'mDataProp': 'pricute' },
            {'mDataProp': 'upload_time' },
            {'mDataProp': 'ticketNo' },
            {'mDataProp': 'added' },
            {'mDataProp': 'add_time' },
            {'mDataProp': 'addIntegral' },
            {'mDataProp': 'integral' }
        ],
        'aoColumnDefs': [
            {
                'aTargets': [4],
                'mData'   : null,
                'mRender' : function (data, type, full) {
                    return pricute_tpl(full);
                }
            },
            {
                'aTargets': [7],
                'mData'   : null,
                'mRender' : function (data, type, full) {
                    return added_tpl(full);
                }
            },
            {
                'aTargets': [9],
                'mData'   : null,
                'mRender' : function (data, type, full) {
                    var context = {classname : 'label-primary', value: full['integral']};
                    return color_tpl(context);
                }
            },
            {
                'aTargets': [10],
                'mData'   : null,
                'mRender' : function (data, type, full) {
                    var context = {classname : 'label-warning', value: full['addIntegral']};
                    return color_tpl(context);
                }
            },
            {
                'aTargets': [11],
                'mData'   : null,
                'mRender' : function (data, type, full) {
                    return action_tpl(full);
                }
            }
        ]
    }, 'data-list');

    $('#search_btn').on('click', function (e) {
        e.preventDefault();
        $listtable.fnReloadAjax();
    });

    var re = /^-?\d+\.?\d{0,1}$/;
    $("#data-list").delegate('a[name="add_intgral"]', 'click', function() {
        var memberId = $(this).attr('data-member');
        var ticketId = $(this).attr('data-ticket');
        if (ticketId && memberId) {
            bootbox.dialog({
                message: alertcontent,
                title: "小票积分",
                buttons: {
                    success: {
                        label: "确定",
                        className: "btn-success",
                        callback: function() {
                            var ticketNoEl = $('.modal-body').find('input[name="ticketNo"]');
                            var integralEl = $('.modal-body').find('input[name="integral"]');
                            var ticketNo = ticketNoEl.val();
                            var integral = integralEl.val();
                            if (!ticketNo) {
                                ticketNoEl.parent().find('.error').remove();
                                ticketNoEl.parent().append(errortip_tpl({tip : '请输入小票号'}));
                                ticketNoEl.css('border-color', '#b94a48');
                                return false;
                            }
                            if (!integral) {
                                integralEl.parent().find('.error').remove();
                                integralEl.parent().append(errortip_tpl({tip : '请输入积分'}));
                                integralEl.css('border-color', '#b94a48');
                                return false;
                            }
                            if (!re.test(integral)) {
                                integralEl.parent().find('.error').remove();
                                integralEl.parent().append(errortip_tpl({tip : '请输入合法数值'}));
                                integralEl.css('border-color', '#b94a48');
                                return false;
                            }
                            var b = false;
                            $.ajax({
                                url : g.ctx + 'customer/ticket/exchange',
                                data: {ticketId : ticketId, memberId : memberId, integral : integral, ticketNo : ticketNo},
                                async: false,
                                success: function(data) {
                                    if (data.status == 'OK') {
                                        //window.location.reload();
                                        $listtable.fnDraw();
                                        b = true;
                                    } else {
                                        ticketNoEl.parent().find('.error').remove();
                                        ticketNoEl.parent().append(errortip_tpl({tip : data.message}));
                                        ticketNoEl.css('border-color', '#b94a48');
                                        b = false;
                                    }
                                }
                            });
                            return b;
                        }
                    },
                    danger: {
                        label: "取消",
                        className: "btn-danger",
                        callback: function() {
                            //Example.show("uh oh, look out!");
                        }
                    }
                }
            });
        }
    });
    $(document).delegate('.bootbox-ticket-input', 'focus', function() {
        $(this).css('border-color', '#e5e5e5');
        $(this).parent().find('.error').remove();
    });
});

